Component({
  /**
   * 组件的属性列表
   */
  properties: {
    Times: [],
  },

  /**
   * 组件的初始数据
   */
  data: {
    scss:'',
    OverTime: [],
    TimeStr:''
  },

  observers: {
    // 监听 OverTime 字段的变化
    'OverTime': function (newValue, oldValue) {
      this.setData({
        TimeStr:this.WtachTimeStr(newValue)+"小时"
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    TimeTap(e) {
      let item = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      // 禁用跳出
      if (item.disabled) return

      // 筛选出已点击的对应索引
      const FilterData = this.data.Times?.map((item, index) => { return { item, index } })?.filter((v) => v.item?.Active === true).map((v) => v?.index)

      // 判断是否可点击并判断边界
      if (
        (index !== FilterData[FilterData.length - 1] + 1 &&
          FilterData.length !== 0 &&
          !FilterData.includes(index) &&
          index !== FilterData[0] - 1
        )) return wx.showToast({
          title: '请连续选择时间',
          icon: 'none'
        })

      //  判断取消边界
      if (FilterData.length > 0 &&
        index !== FilterData[FilterData.length - 1] &&
        index !== FilterData[0] &&
        index !== FilterData[FilterData.length - 1] + 1 &&
        index !== FilterData[0] - 1
      ) return false

      // 选择
      this.data.Times[index].Active = !item.Active

      // 保存已选择item
      const FilterItem = this.data.Times.map((item, index) => { return { item, index } }).filter((v) => v.item.Active === true).map((v) => v.item)


      this.triggerEvent('timeChange', {
        TimeRange:FilterItem.length >= 2 ?`${FilterItem[0].time}-${FilterItem[FilterItem.length - 1].time}` : FilterItem.length > 0 ? FilterItem[0].time : '---',
        duration:this.WtachTimeStr(FilterItem),
        TimeArr:FilterItem
      })
      // 渲染
      this.setData({
        Times: this.data.Times,
        OverTime: FilterItem,
      })
    },


    WtachTimeStr (newValue){
      if(newValue.length < 1) return '----'
      return newValue.length
      let StartStr = newValue[0].time.split(':').map((item) => Number(item))
      let EndStr = newValue[newValue.length - 1].time.split(':').map((item) => Number(item))

      const startDate = new Date(0)
      startDate.setHours(StartStr[0], StartStr[1])
      const endDate = new Date(0)
      endDate.setHours(EndStr[0], EndStr[1])

      const elapsed = endDate - startDate;

      const hours = (elapsed / 3600000);
      // const minutes = Math.floor((elapsed - hours * 3600000) / 60000);


      return hours
    }
  },
  attached() {
    this.setData({
      scss:`--Length:${this.data.Times.length / 2}`
    })
  },
  
})
